﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenCvSharp;
using OpenCvSharp.CPlusPlus;
using System.Windows.Forms;
using System.Diagnostics;

namespace LicenseAutoDetection
{
    class ContractDetection : IDisposable
    {
        CvMemStorage storage;
        CvSURFParams param;
        CvSeq<CvSURFPoint> imagekeypoints;
        CvSeq<IntPtr> imageDescriptors;
        CvRect topROI, bottomROI;

        public ContractDetection()
        {
            storage = Cv.CreateMemStorage(0);
            param = Cv.SURFParams(3000, false);
            imagekeypoints = null;
            imageDescriptors = null;
        }

        public bool Detection(IplImage _src)
        {
            int topKeypoint, bottomKeypoint;
            int heiTen = _src.Height / 10;
            topROI = new CvRect(0, 0, _src.Width, heiTen);                                   // 신청서 윗단 10% 절개
            bottomROI = new CvRect(0, _src.Height - heiTen, _src.Width, _src.Height);        // 신청서 아랫단 10% 절개

            Cv.SetImageROI(_src, topROI);
            topKeypoint = SurfPoint(_src);

            Cv.SetImageROI(_src, bottomROI);
            bottomKeypoint = SurfPoint(_src);

            if (topKeypoint == -1 || bottomKeypoint == -1) return false;

            return ((topKeypoint + bottomKeypoint) > 110) ? true : false;
        }

        public bool Detection(IplImage _src, CvRect RectROI)
        {
            int heiTen = _src.Height / 10;
            Cv.SetImageROI(_src, RectROI);

            return (SurfPoint(_src) > 40) ? true : false;
        }

        private int SurfPoint(IplImage _src)
        {
            try
            {
                Cv2.InitModule_NonFree();
                Cv.ExtractSURF(_src, null, out imagekeypoints, out imageDescriptors, storage, param);

                Cv.ResetImageROI(_src);

                return imagekeypoints.Total;
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message + " " + _src);
                Cv.ResetImageROI(_src);
                return -1;
            }
        }

        #region IDisposable 구현
        private bool disposed = false;

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                if (disposing)
                {
                    if (storage != null) { Cv.ClearMemStorage(storage); Cv.ReleaseMemStorage(storage); }
                    if (param != null) param = null;
                    if (imagekeypoints != null) { imagekeypoints.ClearSeq(); imagekeypoints.Dispose(); }
                    if (imageDescriptors != null) { imageDescriptors.ClearSeq(); imageDescriptors.Dispose(); }
                }

                disposed = true;
            }
        }

        ~ContractDetection()
        {
            Dispose(false);
        }
        #endregion
    }
}